package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.dto.CoursePublishDto;
import com.atguigu.eduservice.entity.frontdto.CourseWebDto;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-12-25
 */
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

    private static final Logger logger = LoggerFactory.getLogger(EduCourseController.class);
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;


    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
        String id=courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("id",id);
    }

    @GetMapping("selectCourseInfo/{courseId}")
    public R selectCourseInfoById(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @PutMapping ("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    /**
     * 根据ID获取课程发布信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getCoursePublishInfo/{id}")
    public R getCoursePublishInfo(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        CoursePublishDto courseInfoForm = courseService.getPublishCourseInfo(id);
        return R.ok().data("item", courseInfoForm);
    }

    /**
     * 课程最终发布
     * @param id
     * @return
     */
    @PutMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     * 课程列表
     * @return
     */
    @GetMapping()
    public R getCourseList(){
        List<EduCourse> list=courseService.list();
        return R.ok().data("list",list);
    }

    /**
     * 条件查询带分页
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> page=new Page<>(current,limit);
        LambdaQueryWrapper<EduCourse> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(courseQuery.getSubjectParentId()),EduCourse::getSubjectParentId,courseQuery.getSubjectParentId());
        queryWrapper.eq(!StringUtils.isEmpty(courseQuery.getSubjectId()),EduCourse::getSubjectId,courseQuery.getSubjectId());
        queryWrapper.eq(!StringUtils.isEmpty(courseQuery.getTeacherId()),EduCourse::getTeacherId,courseQuery.getTeacherId());
        queryWrapper.eq(!StringUtils.isEmpty(courseQuery.getStatus()),EduCourse::getStatus,courseQuery.getStatus());
        queryWrapper.like(!StringUtils.isEmpty(courseQuery.getTitle()),EduCourse::getTitle,courseQuery.getTitle());
        queryWrapper.orderByDesc(EduCourse::getGmtCreate);
        courseService.page(page,queryWrapper);

        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();

        return R.ok().data("total",total).data("records",records);
    }

    /**
     * 删除课程以及相关章节，小结
     * @param courseId
     * @return
     */
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        logger.info("收到的id为：{}",courseId);
        courseService.deleteById(courseId);
        return R.ok();
    }

    /**
     * 根据课程id查询信息,用于feign调用
     * @param id
     * @return
     */
    @PostMapping("getCourseInfos/{id}")
    public com.atguigu.commonutils.CourseWebDtoOrder getCourseInfos(@PathVariable String id){
        CourseWebDto byId = courseService.getBaseCourseInfo(id);

        com.atguigu.commonutils.CourseWebDtoOrder courseInfoVo=new com.atguigu.commonutils.CourseWebDtoOrder();
        BeanUtils.copyProperties(byId,courseInfoVo);
        return courseInfoVo;
    }


}

